package com.ihd.ihardware.view.tzc.health.view;

import android.graphics.Color;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;

import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.widget.ChartMarkerView;
import com.ihd.ihardware.databinding.ActivityWalkHisBinding;
import com.ihd.ihardware.db.StepDataDao;
import com.ihd.ihardware.pojo.StepEntity;
import com.ihd.ihardware.view.tzc.health.viewmodel.WalkViewModel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.wowjoy.commonlibrary.base.BaseActivity;

public class WalkHisActivity extends BaseActivity<ActivityWalkHisBinding, WalkViewModel> {
    private float weight = 0.0f;
    private List<StepEntity> stepEntityList = new ArrayList<>();
    private List<StepEntity> stepList = new ArrayList<>();
    private StepDataDao stepDataDao;
    private YAxis yLAxis;
    private XAxis xAxis;
    private LineData lineData;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(false).init();

        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        weight = getIntent().getFloatExtra("weight", 0.0f);
        binding.chartCV.getLegend().setEnabled(false);
        binding.chartCV.getDescription().setEnabled(false);

        xAxis = binding.chartCV.getXAxis();
        xAxis.setDrawGridLines(false);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextColor(getResources().getColor(R.color.C_99FFFFFF));
        xAxis.setAxisLineColor(getResources().getColor(R.color.C_99FFFFFF));
        xAxis.setAxisLineWidth(0.5f);
        xAxis.setXOffset(5f);

        YAxis yRAxis = binding.chartCV.getAxisRight();
        yRAxis.setDrawAxisLine(true);
        yRAxis.setDrawGridLines(false);
        yRAxis.setTextColor(Color.TRANSPARENT);
        yRAxis.setAxisLineColor(getResources().getColor(R.color.C_00000000));
        yRAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        yRAxis.setXOffset(10f);
        yRAxis.setAxisLineColor(Color.TRANSPARENT);

        yLAxis = binding.chartCV.getAxisLeft();
        yLAxis.setDrawAxisLine(false);
        yLAxis.setDrawGridLines(false);
        yLAxis.setTextColor(Color.TRANSPARENT);
        yLAxis.setXOffset(10f);

        binding.chartCV.setMarker(new ChartMarkerView(this));

        binding.weekTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.weekTV.setTextColor(getResources().getColor(R.color.white));
                binding.weekTV.setBackgroundResource(R.drawable.corners_line_w_16);
                binding.monthTV.setTextColor(getResources().getColor(R.color.C_99FFFFFF));
                binding.monthTV.setBackgroundResource(R.drawable.corners_line_hw_16);
                stepList.clear();
                if (stepEntityList.size() > 0) {
                    int size = stepEntityList.size() >= 7 ? 7 : stepEntityList.size();
                    for (int i = stepEntityList.size() - size; i < stepEntityList.size(); i++) {
                        stepList.add(stepEntityList.get(i));
                    }
                }
                setChartData(stepList);
            }
        });

        binding.monthTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.weekTV.setTextColor(getResources().getColor(R.color.C_99FFFFFF));
                binding.weekTV.setBackgroundResource(R.drawable.corners_line_hw_16);
                binding.monthTV.setTextColor(getResources().getColor(R.color.white));
                binding.monthTV.setBackgroundResource(R.drawable.corners_line_w_16);
                stepList.clear();
                if (stepEntityList.size() > 0) {
                    int size = stepEntityList.size() >= 30 ? 30 : stepEntityList.size();
                    for (int i = stepEntityList.size() - size; i < stepEntityList.size(); i++) {
                        stepList.add(stepEntityList.get(i));
                    }
                }
                setChartData(stepList);
            }
        });

        stepDataDao = new StepDataDao(this);
        stepEntityList.clear();
        stepEntityList.addAll(stepDataDao.getAllDatas());

        stepList.clear();
        if (stepEntityList.size() > 0) {
            int size = stepEntityList.size() >= 7 ? 7 : stepEntityList.size();
            for (int i = stepEntityList.size() - size; i < stepEntityList.size(); i++) {
                stepList.add(stepEntityList.get(i));
            }
        }
        setChartData(stepList);
    }

    private void setChartData(List<StepEntity> mWeightTrendsRes) {
        int totalStep = 0;
        for (int i = 0; i < mWeightTrendsRes.size(); i++) {
            totalStep += Integer.parseInt(mWeightTrendsRes.get(i).getSteps());
        }
        binding.steps.setText(new BigDecimal(totalStep).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
        binding.distance.setText(new BigDecimal(totalStep * 0.0006).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
        binding.calulee.setText(new BigDecimal(weight * totalStep * 0.0006 * 1.036).setScale(1, BigDecimal.ROUND_HALF_UP).toString());
        binding.saveOil.setText(new BigDecimal(totalStep * 0.0006 * 0.07).setScale(2, BigDecimal.ROUND_HALF_UP).toString());

        List<Entry> entries = new ArrayList<>();
        for (int i = 0; i < mWeightTrendsRes.size(); i++) {
            entries.add(new Entry(i, Float.parseFloat(mWeightTrendsRes.get(i).getSteps()), mWeightTrendsRes.get(i).getCurDate()));
        }

        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int IValue = (int) value;
                CharSequence format = DateFormat.format("MM/dd",
                        System.currentTimeMillis() - (long) (entries.size() - IValue) * 24 * 60 * 60 * 1000);
                return format.toString();
            }
        });

        LineDataSet lineDataSet = new LineDataSet(entries, "");
        lineDataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
        lineDataSet.setColor(getResources().getColor(R.color.C_FFFFFF));
        lineDataSet.setLineWidth(3);
        lineDataSet.setCircleColor(getResources().getColor(R.color.C_FFFFFF));
        lineDataSet.setCircleRadius(4);
        lineDataSet.setCircleHoleRadius(2);
        lineDataSet.setValueTextColor(getResources().getColor(R.color.C_00000000));
        lineDataSet.setDrawFilled(true);
        if (lineData == null)
            lineData = new LineData(lineDataSet);
        else {
            lineData.clearValues();
            lineData.addDataSet(lineDataSet);
        }
        binding.chartCV.setData(lineData);

//        xAxis.setLabelCount(mWeightTrendsRes.size(), true);
        if (mWeightTrendsRes.size() == 1) {
            yLAxis.setAxisMinimum(Float.parseFloat(mWeightTrendsRes.get(0).getSteps()) - 10f);
            yLAxis.setAxisMaximum(Float.parseFloat(mWeightTrendsRes.get(0).getSteps()) + 10f);
        } else {
            List num = new ArrayList<>();
            for (int j = 0; j < mWeightTrendsRes.size(); j++) {
                num.add(Float.parseFloat(mWeightTrendsRes.get(j).getSteps()));
            }
            yLAxis.setAxisMinimum((float) Collections.min(num) - 10f);
            yLAxis.setAxisMaximum((float) Collections.max(num) + 10f);
        }

        binding.chartCV.notifyDataSetChanged();
        binding.chartCV.invalidate();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_walk_his;
    }

    @Override
    protected Class getViewModelClass() {
        return WalkViewModel.class;
    }
}
